﻿Imports System.Windows.Forms

Public Class frmCalibrationRpt

    Public Property DefaultCalibration As String = ""
    Public Property DefaultBranch As String = ""

    ''' <summary>Comma-separated list of stations that will be checked by default</summary>
    Public Property DefaultStations As String = ""

    ''' <summary>Comma-separated list of parameters that will be checked by default</summary>
    Public Property DefaultParameters As String = ""

    Private ReadOnly Property Calibration As clsCalibration
        Get
            Return CalibrationTable(cboCalibration.Text).Calibration
        End Get
    End Property

    ''' <summary>
    ''' Return currently selected branch
    ''' </summary>
    Private Function ActiveBranch() As clsBranch
        If cboBranch.SelectedIndex = -1 Then Return Nothing
        Return Calibration.dictBranches(cboBranch.Text)
    End Function

    ''' <summary>
    ''' Return currently selected station
    ''' </summary>
    Private Function ActiveStation() As clsStation
        If ActiveBranch() Is Nothing OrElse cboStation.SelectedIndex = -1 Then Return Nothing
        Return ActiveBranch.dictStations(cboStation.Text)
    End Function

    ''' <summary>
    ''' Return currently selected parameter
    ''' </summary>
    Private Function ActiveParameter() As clsParameter
        If ActiveBranch() Is Nothing OrElse ActiveStation() Is Nothing OrElse cboParameter.SelectedIndex = -1 Then Return Nothing
        Return ActiveStation.dictParameters(cboParameter.Text)
    End Function

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmCalibrationRpt_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
    End Sub

    Private Sub frmCalibrationRpt_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try

            GetWindowPos(REGAPP, Me)
            With cboCalibration
                For Each tbl As clsTable In OpenTables
                    If tbl.TableType = clsTable.enumTableType.Calibration Then .Items.Add(tbl.TableName)
                Next
                If .Items.Contains(DefaultCalibration) Then
                    .Text = DefaultCalibration
                Else
                    .SelectedIndex = IIf(.Items.Count = 0, -1, 0)
                End If
            End With
            cboStyle.SelectedIndex = 0
            btnRefresh.Enabled = True
            btnCopy.Enabled = False

        Catch ex As Exception

            ErrorMsg(, ex)

        End Try
    End Sub

    Private Sub cboCalibration_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboCalibration.SelectedIndexChanged
        Try

            With cboBranch
                .Items.Clear()
                .Items.AddRange(New Generic.List(Of String)(Calibration.dictBranches.Keys).ToArray)
                .SelectedIndex = -1
                If .Items.Contains(DefaultBranch) Then
                    .Text = DefaultBranch
                Else
                    .SelectedIndex = IIf(.Items.Count = 0, -1, 0)
                End If
            End With


        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

    Private Sub cboBranch_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboBranch.SelectedIndexChanged
        Try

            With cboStation
                .Items.Clear()
                .Items.AddRange(New Generic.List(Of String)(ActiveBranch.dictStations.Keys).ToArray)
                .SelectedIndex = -1
                .Text = ""

                .CheckedItemsString = DefaultStations

                'If .Items.Contains(DefaultStation) Then
                '    For i As Integer = 0 To .Items.Count - 1
                '        .SetItemChecked(i, .Items(i) = DefaultStation)
                '    Next
                'End If
            End With


        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

    Private Sub cboStation_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboStation.TextChanged
        Try

            With cboParameter
                .Items.Clear()
                Dim lstParms As New Generic.List(Of String)
                For Each sta As String In cboStation.CheckedItems
                    For Each parm As String In ActiveBranch.dictStations(sta).dictParameters.Keys
                        If Not lstParms.Contains(parm) Then lstParms.Add(parm)
                    Next
                Next
                lstParms.Sort()
                .Items.AddRange(lstParms.ToArray)

                .SelectedIndex = -1
                .Text = ""

                .CheckedItemsString = DefaultParameters

                'If .Items.Contains(_DefaultParameter) Then
                '    For i As Integer = 0 To .Items.Count - 1
                '        .SetItemChecked(i, .Items(i) = _DefaultParameter)
                '    Next
                'End If
            End With


        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

    Private Sub cboParameter_TextChanged(sender As Object, e As EventArgs) Handles cboParameter.TextChanged
        btnRefresh.Enabled = cboParameter.CheckedItems.Count > 0
    End Sub

    Private Sub cboStyle_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cboStyle.SelectionChangeCommitted
        btnRefresh.Enabled = True
    End Sub

    Private Sub btnCopy_Click(sender As Object, e As System.EventArgs) Handles btnCopy.Click
        Try

            Dim Header As String = String.Format("Version:1.0{0}StartHTML:0000000105{0}EndHTML:{1,10:0}{0}StartFragment:0000000105{0}EndFragment:{1,10:0}{0}", vbCrLf, rptHTML.DocumentText.Length + 105)
            Clipboard.SetDataObject(New DataObject(DataFormats.Html, Header & rptHTML.DocumentText))


        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        Try

            Application.UseWaitCursor = True
            Application.DoEvents()

            Dim lstStations As New List(Of String)
            For Each item As String In cboStation.CheckedItems
                lstStations.Add(item)
            Next

            Dim lstParameters As New List(Of String)
            For Each item As String In cboParameter.CheckedItems
                lstParameters.Add(item)
            Next

            With rptHTML
                .DocumentText = Calibration.CreateReport(lstStations, lstParameters, cboStyle.SelectedIndex)
            End With
            'btnRefresh.Enabled = False
            btnCopy.Enabled = True


        Catch ex As Exception

            ErrorMsg(, ex)

        Finally

            Application.UseWaitCursor = False

        End Try

    End Sub

End Class
